package com.jinzhi.jzweb.controller;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jinzhi.api.exception.IFastApiException;
import com.jinzhi.common.annotation.Log;
import com.jinzhi.common.base.BaseController;
import com.jinzhi.common.domain.DictDO;
import com.jinzhi.common.type.ApiEnumError;
import com.jinzhi.common.utils.Result;
import com.jinzhi.jzweb.domain.StuSignUpComDO;
import com.jinzhi.jzweb.domain.TrackDrawDO;
import com.jinzhi.jzweb.domain.TrackDrawRegularDO;
import com.jinzhi.jzweb.service.TrackDrawRegularService;
import com.jinzhi.jzweb.service.TrackDrawService;
import com.jinzhi.jzweb.utils.EasyPoiUtil;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 *
 * <pre>
 * 赛道抽签表（存储抽签信息）
 * </pre>
 * <small> 2023-10-16 16:36:18 | lance</small>
 */
@Controller
@RequestMapping("/jzweb/trackDrawRegular")
public class TrackDrawRegularController extends BaseController {

	@Autowired
	private TrackDrawRegularService trackDrawRegularService;

	@GetMapping()
	@RequiresPermissions("jzweb:trackDrawRegular:trackDrawRegular")
	String TrackDraw(){
	    return "jzweb/trackDrawRegular/trackDrawRegular";
	}


	@GetMapping("/add")
	@RequiresPermissions("jzweb:trackDrawRegular:add")
	String add(){
	    return "jzweb/trackDrawRegular/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("jzweb:trackDrawRegular:edit")
	String edit(@PathVariable("id") Long id,Model model){
		TrackDrawRegularDO trackDrawRegularDO = trackDrawRegularService.selectById(id);
		model.addAttribute("trackDraw", trackDrawRegularDO);
	    return "jzweb/trackDrawRegular/edit";
	}

	@Log("添加赛道抽签表（存储抽签信息）")
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("jzweb:trackDrawRegular:add")
	public Result<String> save( TrackDrawRegularDO trackDraw){
		trackDrawRegularService.insert(trackDraw);
        return Result.ok();
	}

	@Log("修改赛道抽签表（存储抽签信息）")
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("jzweb:trackDrawRegular:edit")
	public Result<String>  update( TrackDrawRegularDO trackDraw){
		boolean update = trackDrawRegularService.updateById(trackDraw);
		return update ? Result.ok() : Result.fail();
	}

	@Log("删除赛道抽签表（存储抽签信息）")
	@PostMapping( "/remove")
	@ResponseBody
	@RequiresPermissions("jzweb:trackDrawRegular:remove")
	public Result<String>  remove( Long id){
		trackDrawRegularService.deleteById(id);
        return Result.ok();
	}

	@Log("批量删除赛道抽签表（存储抽签信息）")
	@PostMapping( "/batchRemove")
	@ResponseBody
	@RequiresPermissions("jzweb:trackDrawRegular:batchRemove")
	public Result<String>  remove(@RequestParam("ids[]") Long[] ids){
		trackDrawRegularService.deleteBatchIds(Arrays.asList(ids));
		return Result.ok();
	}



	/**
     * 赛道抽签 可以对选手固定赛道
	 * @param trackDrawRegularDTO
     * @return
	 */
	@ResponseBody
	@GetMapping("/listTrackDrawRegular")
	@RequiresPermissions("jzweb:trackDrawRegular:trackDrawRegular")
	public Result<List<StuSignUpComDO>> listTrackDrawRegular(TrackDrawRegularDO trackDrawRegularDTO){

		return trackDrawRegularService.selectDrawRegular(trackDrawRegularDTO);
	}

	/**
	 * 查询赛道信息
	 * @param dictDO
	 * @return
	 */
	@ResponseBody
	@PostMapping("/dictDOList")
//	@RequiresPermissions("jzweb:trackDrawRegular:trackDrawRegular")
	public Result<List<DictDO>> dictDOList(@RequestBody DictDO dictDO){

		return Result.ok(trackDrawRegularService.dictDOList(dictDO));
	}


	/**
	 * 查询上次的抽签结果
	 */
	@ResponseBody
	@GetMapping("/trackRegularList")
	public Result<TrackDrawRegularDO> trackRegularList(TrackDrawRegularDO trackDrawDTO){

		return Result.ok(trackDrawRegularService.trackRegularList(trackDrawDTO));
	}


	/**
	 * 导出抽签结果信息
	 *
	 * @param response
	 */
	@Log("导出抽签结果信息")
	@RequestMapping("/downloadImportTemplate")
	@RequiresPermissions("jzweb:trackDrawRegular:import")
	@ResponseBody
	public void downloadImportTemplate(HttpServletRequest request, HttpServletResponse response, TrackDrawDO trackDrawDTO) {
		Wrapper<TrackDrawRegularDO> wrapper = new EntityWrapper<>();
		wrapper.orderBy(true,"sort");
		List<TrackDrawRegularDO> trackDrawDOS = trackDrawRegularService.selectList(wrapper);
		if (trackDrawDOS != null && trackDrawDOS.size() > 0) {
			//导出操作
			EasyPoiUtil.exportExcel(trackDrawDOS, "赛道抽签信息", "赛道抽签信息", TrackDrawRegularDO.class,  "赛道抽签信息", response);
		} else {
			throw new IFastApiException(ApiEnumError.NOTBATCHSTU.getCodeStr());
		}
	}



}
